<?
require_once("cms/core/action/AbstractListAction.php");
require_once("cms/download/om/DownloadRevisionPeer.php");
require_once("cms/core/om/BinaryFilePeer.php");

class ListAction extends AbstractListAction {

    private $log;

    public function __construct() {
        $this->log =& LoggerManager::getLogger("ListAction");
    }

    public function activate(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Activating downloads");
        $c = new Criteria();
        $c->add(DownloadPeer::DOWNLOAD_ID, $form->getItems(), Criteria::IN);
        $c->add(DownloadPeer::ACTIVE, true);
        try {
            DownloadPeer::doUpdate($c);
            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.activate", true));
        } catch(PropelException $e) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.activate", true));
            $this->log->error($e->getMessage());
        }

    }

    public function deactivate(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Deactivating downloads");
        $c = new Criteria();
        $c->add(DownloadPeer::DOWNLOAD_ID, $form->getItems(), Criteria::IN);
        $c->add(DownloadPeer::ACTIVE, false);
        try {
            DownloadPeer::doUpdate($c);
            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.deactivate", true));
        } catch(PropelException $e) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.deactivate", true));
            $this->log->error($e->getMessage());
        }

    }

    public function delete(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $this->log->debug("Deleting downloads");
        $c = new Criteria();
        $c->add(DownloadRevisionPeer::DOWNLOAD_ID, $form->getItems(), Criteria::IN);

        $conn = Propel::getConnection();

        try {

            $conn->begin();

            $revisions = DownloadRevisionPeer::doSelect($c, $conn);
            foreach($revisions as $revision) {
                DownloadRevisionPeer::doDelete($revision, $conn);
                BinaryFilePeer::doDelete($revision->getBinaryFile(), $conn);
            }

            $c = new Criteria();
            $c->add(DownloadPeer::DOWNLOAD_ID, $form->getItems(), Criteria::IN);
            $downloads = DownloadPeer::doSelect($c, $conn);

            foreach($downloads as $download) {

                // Move all higher positions one up.
                $query =
                "UPDATE download SET position = position - 1 ".
                "WHERE category_id = " .$download->getCategoryId()." ".
                "AND position > " . $download->getPosition();

                $stmt = $conn->createStatement();
                $stmt->executeUpdate($query, $conn);

                DownloadPeer::doDelete($download, $conn);
            }

            $conn->commit();

            $this->messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.success.delete", true));

        } catch(PropelException $e) {
            $conn->rollback();
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.delete", true));
            $this->log->error($e->getMessage());
        }

    }

    public function moveup(ActionConfig &$mapping, &$form, Request &$request,
            Response &$response) {
        $this->move($form, "up");
    }

    public function movedown(ActionConfig &$mapping, &$form, Request &$request,
            Response &$response) {
        $this->move($form, "down");
    }

    private function move($form, $direction) {

        $items = $form->getItems();

        $download = DownloadPeer::retrieveByPk($items[0]);

        if(is_null($download)) {
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE, new ActionMessage("Error finding download."));
            return;
        }

        $conn = Propel::getConnection();
        $stmt = $conn->createStatement();

        try {

            $conn->begin();

            $query =
                "UPDATE download SET position = position ".($direction == "down" ? "-" : "+")." 1 ".
                "WHERE ".
                "category_id = ".$download->getCategoryId()." ".
                "AND position = ".($direction == "down" ? $download->getPosition() + 1 : $download->getPosition() - 1);

            $stmt->executeUpdate($query, $conn);

            $query =
                "UPDATE download SET position = position ".($direction == "down" ? "+" : "-")." 1 ".
                "WHERE ".
                "category_id = ".$download->getCategoryId()." ".
                "AND download_id = ".$download->getDownloadId();

            $stmt->executeUpdate($query, $conn);

            $conn->commit();

        } catch(PropelException $e) {
            $conn->rollback();
            $errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("list.error.move", true));
            $this->log->error($e->getMessage());
        }

    }


}
?>